﻿using BCL.Tool.Extend;
using BCL.WPFComponent.Base;
using Devices;
using DevicesView.Entity;
using DevicesView.Logic.Test;
using DevicesView.Model.ViewData;
using System;
using System.Linq;
using System.Collections.Generic;
using System.IO;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media.Imaging;
using BCL.Tool;
using DevicesView.Logic.App;
namespace DevicesView
{
    /// <summary>
    /// UCRelust.xaml 的交互逻辑
    /// </summary>
    public partial class UCRelust : BaseFunctionControl
    {

        private ResultLogic logic;
        private List<Doctor> ds;
        private List<BitmapImage> images;
        public UCRelust()
        {
            InitializeComponent();
            logic = new ResultLogic();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            images = new List<BitmapImage>
            {
                File.ReadAllBytes("image/print.png").ToBitmap()
            };
            //ds = logic.GetOrgDoctors();
            //ds = XiaonuanPMS.Logic.TestingsLogic.Doctors;
            if (ds == null)
            {
                ds = new List<Doctor>();
                ds.Insert(0, new Doctor { Code = "", Name = "所有".Language() });
                if(AppResource.CurrentResource.Doctors!=null)
                foreach (var item in AppResource.CurrentResource.Doctors)
                    ds.Add(item);
            }


            comboBoxEdit1.ItemsSource = ds;
            comboBoxEdit1.DisplayMemberPath = "Name";
            comboBoxEdit1.SelectedIndex = 0;


            dateEditStart.SelectedDate = DateTime.Today;
            dateEditEnd.SelectedDate = DateTime.Today.AddDays(1);

            SelectResultData();
        }

        private void SelectResultData()
        {
            Doctor dc = ds[comboBoxEdit1.SelectedIndex];//. as Doctor;
            List<TestResult> testResults= logic.GetResult(dc.Code, (DateTime)dateEditStart.SelectedDate, (DateTime)dateEditEnd.SelectedDate);
            testResults = testResults?.OrderByDescending(o => long.Parse(o.CMD?.Code==null? "0":o.CMD.Code)).ToList();
            testResults?.ForEach(o => o.Images = images);
            this.gridControl1.ItemsSource = testResults;
        }



        private void Image_MouseDown(object sender, MouseButtonEventArgs e)
        {

            TestResult testResult = gridControl1.SelectedItem as TestResult;
            Devices.IDevices dev = Devices.DevicesCollection.CreateDevices("print\\temp.xml", testResult.Info.ClassInfo);
            dev.PrintTool = Logic.Print.PrintLogic.GetPrintConfig(dev.Info);
            dev.PrintTool.Print(new List<Result> { testResult });

            //else if (e.Column == gridColumnDiagram)
            //{
            //    TestResult cc = gridView1.GetRow(e.RowHandle) as TestResult;
            //    List<TestResult> results = gridControl1.DataSource as List<TestResult>;

            //    FunctionEntity fe = new FunctionEntity();
            //    fe.FunctionType = FunctionType.Control;
            //    string id = DateTime.Now.Ticks.ToString();
            //    DevicesView.Logic.App.AppResource.Results.Add(id, results.Where<Result>(o => o.CMD.PetId == cc.CMD.PetId).ToList());
            //    fe.ItemParamter = "http://www.huimopei.com:8888/chart/" + id;
            //    //fe.ItemParamter = string.Format(@"file:\\\{0}dist\index.html#/chart/{1}",AppDomain.CurrentDomain.BaseDirectory,id);
            //    fe.Title = cc.Pet + "检查";
            //    fe.Control = new DevicesView.Web.WebControl(fe);
            //    BCLApplication.MainForm.OpenFunction(fe);





            //FunctionEntity fe = new FunctionEntity();
            //fe.FunctionType = FunctionType.Control;
            //fe.ItemParamter = results.Where<Result>(o => o.CMD.PetId == cc.CMD.PetId).ToList();
            //fe.Title = cc.Pet + "检查趋势";
            //fe.Control= new Diagram.UCResultChartWeb(fe);
            //BCLApplication.MainForm.OpenFunction(fe);

            //Diagram.UCResultChart rc = new Diagram.UCResultChart(results.Where<Result>(o => o.CMD.PetId == cc.CMD.PetId).ToList());
            //rc.ShowDialog(this);
        }

        public override void LanguageBinding()
        {
          
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            SelectResultData();
        }

        private void GridControl1_Selected(object sender, System.Windows.Controls.SelectedCellsChangedEventArgs e)
        {
            TestResult result= gridControl1.SelectedItem as TestResult;
            ucRelustDisplay.Result = result;
            if (result == null)
                textBlockPetName.Text = "";
            else
                textBlockPetName.Text = string.Format("{0}-{1}-{2}",result.Pet,result.CMD.Id,result.CMD.Name);
        }
    }
}

